﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BbsbWebService.Classes;
using BbsbWebService;
using BbsbWebService.Repositories;

namespace BbsbWsGui
{
    /// <summary>
    /// Interaction logic for GroupsTab.xaml
    /// </summary>
    public partial class GroupsTab : UserControl
    {
        public GroupsTab()
        {
            InitializeComponent();
        }

        #region Group Methods

        private void groupsLst_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateGroupItems();
        }

        private void UpdateGroupItems()
        {
            Group selectedItem = groupsLst.SelectedItem as Group;
            if (selectedItem != null)
            {
                itemsLst.ItemsSource = selectedItem.GroupItems.ToList();
                itemsLst.Items.Refresh();
            }
        }

        private void addGroupBtn_Click(object sender, RoutedEventArgs e)
        {
            AddGroupWindow window = new AddGroupWindow();
            if (window.ShowDialog() == true &&
                window.Group != null)
            {
                RepositoryInstances.GroupRepository.SaveItem(window.Group);
                //RefreshItems();
            }
        }

        private void editGroupBtn_Click(object sender, RoutedEventArgs e)
        {
            if (groupsLst.SelectedItem != null)
            {
                Group group = groupsLst.SelectedItem as Group;
                if (group != null)
                {
                    AddGroupWindow window = new AddGroupWindow(group.Clone());
                    if (window.ShowDialog() == true &&
                        window.Group != null)
                    {
                        RepositoryInstances.GroupRepository.SaveItem(window.Group);
                        //RefreshItems();
                    }
                }
            }
        }

        private void removeGroupBtn_Click(object sender, RoutedEventArgs e)
        {
            if (groupsLst.SelectedItem != null)
            {
                Group group = groupsLst.SelectedItem as Group;
                if (group != null)
                {
                    RepositoryInstances.GroupRepository.RemoveItem(group);
                    //RefreshItems();
                }
            }
        }

        #endregion

        public bool Enabled
        {
            set
            {
                bool enabled = value;
                groupsLst.IsEnabled = enabled;
                addGroupBtn.IsEnabled = enabled;
                editGroupBtn.IsEnabled = enabled;
                removeGroupBtn.IsEnabled = enabled;
            }
        }

        public ObservableCollection<Group> ItemsSource
        {
            set
            {
                groupsLst.ItemsSource = value;
            }
        }
    }
}
